//
//  DeviceCell.swift
//  DSP
//
//  Created by tucici on 2024/5/22.
//

import UIKit
import CoreBluetooth
import TCCKit
class DeviceCell: UITableViewCell {
    private var name : UILabel!
    private var mac : UILabel!
    private var sub : UILabel!
    private var on : UISwitch!
    var line : UIView!
    var block : ((Bool)->Void)?
    var longPressBlock : (()->Void)?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        selectionStyle = .none
        backgroundColor = .clear
        
        name = UILabel()
        name.text = "测试测试"
        name.font = TCCKit.font(16)
        name.textHexColor = "#000000"
        name.minimumScaleFactor = 0.5
        name.adjustsFontSizeToFitWidth = true
        name.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        name.setContentHuggingPriority(.defaultLow, for: .horizontal)

        mac = UILabel()
        mac.text = "mac"
        mac.font = TCCKit.font(12)
        mac.textHexColor = "#999999"
        
        sub = UILabel()
        sub.text = "Custom Name"
        sub.font = TCCKit.font(14)
        sub.textHexColor = "#000000"
        sub.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        sub.setContentHuggingPriority(.defaultHigh, for: .horizontal)

        on = UISwitch()
        on.addTarget(self, action: #selector(acionForSwitch), for:.valueChanged)
        on.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        
        line = UIView()
        line.bgHexColor = "#000000"
        
        contentView.addSubview(name)
        contentView.addSubview(mac)
        contentView.addSubview(sub)
        contentView.addSubview(on)
        contentView.addSubview(line)
        
        name.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        
        mac.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        
        sub.snp.makeConstraints { make in
            make.height.equalTo(16)
            make.top.bottom.equalToSuperview().inset(14)
            make.left.equalTo(name.snp.right).offset(10)
        }
        
        on.snp.makeConstraints { make in
            make.left.equalTo(sub.snp.right).offset(10)
            make.right.equalToSuperview().inset(10)
            make.centerY.equalToSuperview()
        }
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    var peripheral : DSPeripheral!{
        didSet{
            name.text = peripheral.peripheral?.name ?? ""
            mac.text = peripheral.peripheral?.mac ?? ""
            mac.font = TCCKit.font(name.text == "" ? 16 : 12)
            on.isOn = peripheral.peripheral?.state == .connected
            sub.text = peripheral.name
            name.snp.updateConstraints { make in
                make.centerY.equalToSuperview().offset(mac.text?.count ?? 0 <= 0 ? 0 : -7)
            }
            mac.snp.updateConstraints { make in
                make.centerY.equalToSuperview().offset(name.text?.count ?? 0 <= 0 ? 0 : 7)
            }
        }
    }
    @objc private  func acionForSwitch(sender:UISwitch){
        block?(sender.isOn)
    }
    
    
    lazy var longpress : UILongPressGestureRecognizer = {
        let long = UILongPressGestureRecognizer(target: self, action: #selector(actionForlongPress))
        contentView.addGestureRecognizer(long)
        return long
    }()
    
    @objc func actionForlongPress(_ sender:UILongPressGestureRecognizer){
        if sender.state == .began{
            longPressBlock?()  
            let feedbackGenerator = UIImpactFeedbackGenerator(style: .medium)
            feedbackGenerator.impactOccurred()
        }
    }
}
